<?php
/**
* Page comment.
*
* Copyright (c) 2009 James Gauld <james@jamesgauld.net>
* This file is part of Scribble.
* @license http://www.scribblecms.co.uk/license.txt
*
* @package Scribble
*/
class PageCommentModel extends ScribbleModel {

	/**
	* Returns this comment's text body in a format appropriate for displaying
	* within an HTML document.
	*
	* Links beginning with "http" or "www" are converted to active hyperlinks with
	* rel="nofollow external"
	*
	* @return string
	*/
	public function getBodyHtml() {

		// Convert to HTML
		$html = htmlentities($this->body, ENT_COMPAT, 'UTF-8');
		$html = '<p>'.str_replace("\n", "<br/>", str_replace("\n\n", "</p><p>", str_replace("\r", "", $html))).'</p>';

		// Inject hyperlinks
		$html = preg_replace("/(http:\/\/[a-z0-9\-_:\/\%&=;\.\?]+)/ims", '<a href="$1" rel="external nofollow">$1</a>', $html);
		return $html;
	}

	/**
	* Returns the URL of this comment's author.
	*
	* @param string Size of the gravatar icon
	* @param string The default URL to use if no gravatar was found
	* @return string
	*/
	public function getGravatarUrl($s=NULL, $d=NULL) {
		$url = 'http://www.gravatar.com/avatar/';
		$url .= md5(strtolower(preg_replace("/[\s\r\n\t]/ms", "", $this->authoremail))).".jpg";
		$query = array();
		if($s!==NULL) {
			$query[] = "s={$s}";
		}
		if($d!==NULL) {
			$query[] = "d=".urlencode($d);
		}
		if(!empty($query)) {
			$url .= "?".implode("&", $query);
		}
		return $url;
	}
}
?>